package cn.shadow.module.snake;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Array;

import java.util.Iterator;

public class Shoot extends Stage {

    private TextureRegion bullet;
    private Food food;
    private Snake snake;
    private int speed; //子弹速度
    private Sound hit;
    private Sound shoot;
    public Shoot() {
        Texture texture = new Texture(Gdx.files.internal("image/bullet.png"));
        this.hit = Gdx.audio.newSound(Gdx.files.internal("sound/shoot4.mp3"));
        this.shoot = Gdx.audio.newSound(Gdx.files.internal("sound/shoot3.mp3"));
        this.bullet = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());
        this.speed = 20;
    }



    public Sound getHit() {
        return hit;
    }

    public void setHit(Sound hit) {
        this.hit = hit;
    }

    public Sound getShoot() {
        return shoot;
    }

    public void setShoot(Sound shoot) {
        this.shoot = shoot;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        if (speed > 100) {
            speed = 50;
        } else if (speed < 5) {
            speed = 5;
        }
        this.speed = speed;
    }

    public TextureRegion getBullet() {
        return bullet;
    }

    public void setBullet(TextureRegion bullet) {
        this.bullet = bullet;
    }

    public Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    public Snake getSnake() {
        return snake;
    }

    public void setSnake(Snake snake) {
        this.snake = snake;
    }

    public Bullet shoot(float x, float y, SnakeNode.Direction direction) {
        Bullet bullet = new Bullet(x, y, this.bullet, direction, snake.getMapX(), snake.getMapX(), speed);
        addActor(bullet);
        return bullet;
    }

    public void checkHit() {
        int hitCount = 0;
        Array<Actor> removeBullet = new Array<>();
        Array<Actor> removeFood = new Array<>();
        for (Iterator<Actor> iterator = getActors().iterator(); iterator.hasNext(); ) {
            Actor bullet = iterator.next();
            Array<Actor> foods = food.getActors();
            for (Iterator<Actor> foodIt = foods.iterator(); foodIt.hasNext(); ) {
                Actor foodNode = foodIt.next();
                FoodNode node = (FoodNode) foodNode;
                if (node.canEat(bullet.getX(), bullet.getY(), bullet.getWidth()) && !removeBullet.contains(bullet,true) && !removeFood.contains(node,true)) {
                    snake.growUp(node.getRegion());
                    hit.play();
                    removeBullet.add(bullet);
                    removeFood.add(node);
                    System.out.println("子弹击中了一个目标，目标消失，size=" + food.getActors().size);
                    hitCount++;
                }
            }
        }
        Array<Actor> bullets = getActors();
        Array<Actor> foods = food.getActors();
        bullets.removeAll(removeBullet, true);
        foods.removeAll(removeFood, true);
        if (hitCount > 0) {
            for (int i = 0; i < hitCount; i++) {
                food.newFood();
            }
        }
    }
}
